﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace V5_Apps.Plugins.TorrentSearch.Core {
    /// <summary>
    /// 文件处理
    /// </summary>
    public class TaskHelper<T> {
        /// <summary>
        /// 任务任务
        /// </summary>
        /// <param name="doObj"></param>
        public delegate void OutTaskDoWorkHandler(T doObj);
        /// <summary>
        /// 任务数据
        /// </summary>
        /// <param name="dataObj"></param>
        public delegate void OutTaskDataWorkHandler(Queue<T> _queue);

        public OutTaskDoWorkHandler OutTaskDoWorkDelegate;
        public OutTaskDataWorkHandler OutTaskDataWorkDelegate;

        //线程是否运行
        public bool IsStop = true;
        //多线程个数
        public int ThreadCount = 1;

        private Thread[] _ths;//单线程
        private Thread _th;//多线程
        private Queue<T> _queue = new Queue<T>();

        //启动任务
        public void Start() {
            if (this.IsStop) {
                this.IsStop = false;
                _ths = new Thread[ThreadCount];
                for (int i = 0; i < ThreadCount; i++) {
                    _ths[i] = new Thread(Run_Ths);
                    _ths[i].Start(i);//传入object类型数据
                }
                //
                _th = new Thread(Run_Th);
                _th.Start();
            }
        }


        //任务执行 多线程  实际处理数据
        private void Run_Ths(object args) {
            //do something
            int stepTime = 100;
            while (true) {
                //while something
                if (this.IsStop) {
                    break;
                }
                lock (_queue) {
                    if (_queue != null && _queue.Count > 0) {
                        //队列出栈  先进先出
                        T s = _queue.Dequeue();
                        if (OutTaskDoWorkDelegate != null) {
                            OutTaskDoWorkDelegate(s);
                        }
                    }
                    else {
                        stepTime = 500;
                    }
                }
                Thread.Sleep(stepTime);
            }
        }
        //停止任务
        public void Stop() {
            this.IsStop = true;

            if (_ths != null) {
                for (int i = 0; i < ThreadCount; i++) {
                    _ths[i].Abort();
                }
            }

            if (_th != null) {
                _th.Abort();
            }

        }
        //单线程 用于取数据库数据
        private void Run_Th(object args) {
            //do something
            while (true) {
                //while something
                if (this.IsStop) {
                    break;
                }
                lock (_queue) {
                    //进入队列  先进先出
                    if (OutTaskDataWorkDelegate != null) {
                        OutTaskDataWorkDelegate(_queue);
                    }
                    //_queue.Enqueue("数据:" + loop.ToString() + " " + Guid.NewGuid().ToString());
                }
                Thread.Sleep(10);
            }
        }
    }
}
